<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function get_filtre()
	{
		if (habilitation())
		{
			$retour = array();
			$champs = champs();
			foreach ($champs as $champ)
			{
				if ($champ["filtre"])
				{
					$retour[$champ["id"]]["nom"] = trad($champ["Affichage"], APPLNG);
					$retour[$champ["id"]]["id"] = $champ["id"];
					$retour[$champ["id"]]["type"] = $champ["type"];
					$retour[$champ["id"]]["typeselect"] = $champ["typeselect"];
					$retour[$champ["id"]]["data"] = $champ["data"];
				}
			}
			
			return $retour;
		}
	}
	
	function gridhtml($table, $numpage, $tri, $ordre)
	{
		if (habilitation())
		{
			$php = '
					$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
					GP_ActiveRecord::setDefaultDBConnection($gp_db);
					
					if($champ == "id_sample")
					{
						$sel = new GP_ActiveRecord("_fo_samples");
						$select = array(
							"name"
						);
						$tab_sel = $sel -> get($select, "language = \'en\' && id = " . $ligne[$champ]);
						foreach ($tab_sel as $sel_u)
							$ligne[$champ] = sl(utf8_encode($sel_u["name"]));
					}
					
					if($champ == "id_country")
					{
						$sel = new GP_ActiveRecord("_fo_samples_countries");
						$select = array(
							"name"
						);
						$tab_sel = $sel -> get($select, "language = \'en\' && id = " . $ligne[$champ]);
						foreach ($tab_sel as $sel_u)
							$ligne[$champ] = sl(utf8_encode($sel_u["name"]));
					}
			';
			
			$champs = champs();
			$retour["id"] = 'id';
			$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
			if (strlen(get_s('requete_' . $table)) < 1)
				$requete = "SELECT * FROM $table";
			else
			{
				$cond = get_s('requete_' . $table);
				$cond = make_cond($cond);
				$requete = "SELECT * FROM $table WHERE $cond";
			}
			
			if (!strlen($cond))
				$cond = "1 = 1";
			
			$res = $gp_db -> fetchArray($requete);
			$total = count($res);
			if ($total < 1)
				$retour["vide"] = true;
			else
				$retour["vide"] = false;
			$debut = ($numpage * RESPARPAGE) - RESPARPAGE;
			$retour["total"] = $total;
			$retour["pages"] = ceil($total / RESPARPAGE);
			
			$affichage = array();				
			$champs_tri = array();				
			
			$val = '';
			
			$champsql = array();
			$champsql[] = $retour['id'];
			$i = 0;
			foreach ($champs as $champ)
			{
				$affichage[$champ['id']] = trad($champ['Affichage'], APPLNG);
				$val .= $champ['id'] . ', ';
				$champsql[] = $champ['id'];
				if ($champ["tri"] == true)
					$champs_tri[] = $champ['id'];
				$i++;
			}
			$width = round(85 / $i);
			$val .= $retour["id"];
			
			$requete = "SELECT $val FROM $table WHERE $cond ORDER BY $tri $ordre LIMIT $debut, " . RESPARPAGE;
			sup_s('export_' . $table);
			list($requete_export, $dummy) = explode(' ORDER BY ', $requete, 2);
			$requete_export = str_replace($val, '*', $requete_export);
			s('export_' . $table, $requete_export);
			
			list($dummy, $conditions) = explode(' WHERE ', $requete, 2);
			GP_ActiveRecord::setDefaultDBConnection($gp_db); 
			$datas = new GP_ActiveRecord($table);
	
			$tab_data = $datas -> get($champsql, $conditions);
			$retour["res"] = $tab_data;
			$retour["champs_tri"] = $champs_tri;
			$retour["php"] = $php;
			$retour["affichage"] = $affichage;
			$retour["width"] = $width;
			$retour["tri"] = $tri;
			$retour["ordre"] = $ordre;
			$retour["limite"] = RESPARPAGE;
			$retour["numpage"] = $numpage;
			
			$retour["autre_ordre"] = ($ordre == 'ASC') ? 'DESC' : 'ASC';
			
			return $retour;
		}
	}
	
	function champs()
	{
		$retour = array();
		
		$retour['id_sample']['id'] = 'id_sample';
		$retour['id_sample']['Affichage'] = 'Echantillon';
		$retour['id_sample']['type'] = 'select';
		$retour['id_sample']['typeselect'] = 'sql';
		$retour['id_sample']['data'] = "SELECT id, name FROM _fo_samples WHERE language = 'en'";
		$retour['id_sample']['premierchoix'] = trad('Choisir', APPLNG) . '%%';
		$retour['id_sample']['defaut'] = '';
		$retour['id_sample']['js'] = 'return false;';
		$retour['id_sample']['obligatoire'] = true;
		$retour['id_sample']['tri'] = true;
		$retour['id_sample']['filtre'] = true;
		
		$retour['id_country']['id'] = 'id_country';
		$retour['id_country']['Affichage'] = 'Pays';
		$retour['id_country']['type'] = 'select';
		$retour['id_country']['typeselect'] = 'sql';
		$retour['id_country']['data'] = "SELECT id, name FROM _fo_samples_countries WHERE language = 'en'";
		$retour['id_country']['premierchoix'] = trad('Choisir', APPLNG) . '%%';
		$retour['id_country']['defaut'] = '';
		$retour['id_country']['js'] = 'return false;';
		$retour['id_country']['obligatoire'] = true;
		$retour['id_country']['tri'] = true;
		$retour['id_country']['filtre'] = true;
		
		
		$retour['stock']['id'] = 'stock';
		$retour['stock']['Affichage'] = 'Stock';
		$retour['stock']['max'] = 11;
		$retour['stock']['type'] = 'input';
		$retour['stock']['obligatoire'] = true;
		$retour['stock']['tri'] = true;
		$retour['stock']['filtre'] = true;
		
		$retour['used']['id'] = 'used';
		$retour['used']['Affichage'] = 'Utilisés';
		$retour['used']['max'] = 11;
		$retour['used']['type'] = 'input';
		$retour['used']['obligatoire'] = true;
		$retour['used']['tri'] = true;
		$retour['used']['filtre'] = true;
		
		$retour['differential']['id'] = 'differential';
		$retour['differential']['Affichage'] = 'Restant';
		$retour['differential']['max'] = 11;
		$retour['differential']['type'] = 'input';
		$retour['differential']['obligatoire'] = true;
		$retour['differential']['tri'] = true;
		$retour['differential']['filtre'] = true;

		return $retour;
	}
	
	$gp_request = GP_secure::getInstance();
	$tri = (!strlen($GLOBALS['tri'])) ? $gp_request -> Get('tri', 'id_sample') : $GLOBALS['tri'];
	$ordre = (!strlen($GLOBALS['ordre'])) ? $gp_request -> Get('ordre', 'ASC') : $GLOBALS['ordre'];
	$numpage = (!strlen($GLOBALS['numpage'])) ? $gp_request -> Get('numpage', 1) : $GLOBALS['numpage'];
	$requete = $gp_request -> Get('requete', $GLOBALS['requete']);
	$delfiltre = $gp_request -> Get('delfiltre', $GLOBALS['delfiltre']);
	
	if ($delfiltre == 1)
		sup_s('requete_' . $this -> table);
	
	if (strlen($requete))
	{
		sup_s('requete_' . $this -> table);
		s('requete_' . $this -> table, $requete);
	}
	
	$this -> setvar('titre', 'Liste des échantillons par pays');
	$this -> setvar('retour_url', rw('echantillons'));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('get_filtre', get_filtre());
	$this -> setvar('gridhtml', gridhtml($this -> table, $numpage, $tri, $ordre));
?>